package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.mapper.ScoreMapper;
import com.example.demo.entity.Message;
import com.example.demo.entity.Score;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class GetNumController {
    @Autowired
    private ScoreMapper sm;
    @RequestMapping("/getnum")
    public void getNum(HttpServletResponse response) throws IOException {
        List<Score> list= new ArrayList<>();
        list=sm.findAllScore();
        Map<String, Integer> failCountMap = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            Score score = list.get(i);
            String studentName = score.getName();
            int scoreValue = score.getFscore();
            if (scoreValue < 60) {
                if (failCountMap.containsKey(studentName)) {
                    int failCount = failCountMap.get(studentName);
                    failCountMap.put(studentName, failCount + 1);
                } else {
                    failCountMap.put(studentName, 1);
                }
            }else{
                if (!failCountMap.containsKey(studentName)) {
                    failCountMap.put(studentName,0);
                }
            }
        }
        response.getWriter().write(JSONObject.toJSONString(Message.success(failCountMap)));
    }
}
